<template>
  <div class="myFollow" style="padding-top: 10px">
    <el-table :data="tableData" border style="width: 100%; padding: 20px">
      <el-table-column prop="name" label="教师名称"> </el-table-column>
      <el-table-column prop="Gender" label="性别"> </el-table-column>
      <el-table-column prop="fansNum" label="粉丝数目"> </el-table-column>
      <el-table-column prop="operation" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="deleteFollow(scope.$index, scope.row)"
            >取消关注</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { schools, taechers } from "../../../public/schoolData.js";
export default {
  name: "studentFollow",
  data: function () {
    return {
      seen: true,
      studentId: JSON.parse(sessionStorage.getItem("studentInfo")).studentId,
      tableData: [],
      // followTeachers: schools[0].followTeachers,
    };
  },
  created() {
    this.round();
  },
  computed: {
    // tableData: () => {
    //   return schools.find((data) => {
    //     return (
    //       data.id == JSON.parse(sessionStorage.getItem("studentInfo")).studentId
    //     );
    //   }).followTeachers;
    // },
    // tableData: () => {
    //   return schools
    //     .find((data) => {
    //       return (
    //         data.id ==
    //         JSON.parse(sessionStorage.getItem("studentInfo")).studentId
    //       );
    //     })
    //     .followTeachers.forEach((data) => {
    //       let queryResult = taechers.some((item) => {
    //         return data.id == item.id;
    //       });
    //       if (queryResult) {
    //         return data;
    //       }
    //     });
    // },
  },
  methods: {
    round() {
      this.tableData = [];
      schools
        .find((data) => {
          return data.id == this.studentId;
        })
        .followTeachers.forEach((data) => {
          let queryResult = taechers.some((item) => {
            return data.id == item.id;
          });
          if (queryResult) {
            this.tableData.push(data);
          }
        });
    },
    //点击取消关注，删除当前内容，并将修改后的数据更改到schoolData.js当中
    deleteFollow(index, row) {
      this.$confirm("此操作将取消对该老师的关注, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "取消成功!",
            //取消课程
          });
          let followTeachersA = schools.find((data) => {
            return (
              data.id ==
              JSON.parse(sessionStorage.getItem("studentInfo")).studentId
            );
          }).followTeachers;
          // let indexDel = followTeachersA[index];
          // console.log("followTeachersA===" + followTeachersA);
          followTeachersA.splice(index, 1);
          taechers.find((data) => {
            return data.id == row.id;
          }).fansNum--;
          this.round();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作",
          });
        });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
</style>
